﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using DragDraw.Util;

namespace DragDraw.Tool
{
    public partial class DiaryLargeViewer : UserControl
    {
        private Diary m_curDiary;
        private int m_diaryIndex = -1;

        internal DiaryMenu MainApp { get; set; }

        public DiaryLargeViewer()
        {
            InitializeComponent();
        }

        public void Init()
        {
            if (WidgetHelper.IsInMySpace == false)
            {
                ControlPanel.Visibility = Visibility.Collapsed;
                TxtMain.Visibility = Visibility.Collapsed;
                TxtTitle.Visibility = Visibility.Collapsed;
            }
            else
            {
                ControlPanel.Visibility = Visibility.Visible;
                TxtMain.Visibility = Visibility.Visible;
                TxtTitle.Visibility = Visibility.Visible;
            }
            if (m_diaryIndex >= 0)
            {
                m_curDiary = MainApp.Diaries[m_diaryIndex];
            }
            RefreshLargeViewer();
        }

        public void RefreshLargeViewer()
        {
            if (m_curDiary != null)
            {
                TxtTitle.Text = m_curDiary.Title;
                TxtMain.Text = m_curDiary.Content;
                TbTitle.Text = m_curDiary.Title;
                TbMain.Text = m_curDiary.Content;
            }
            else
            {
                TxtTitle.Text = "";
                TxtMain.Text = "";
                TbTitle.Text = "";
                TbMain.Text = "";
            }
        }

        public void SetDiary(int index)
        {
            m_diaryIndex = index;
        }

        private void BtnNew_Click(object sender, RoutedEventArgs e)
        {
            m_curDiary = new Diary() { Title = "", Content = ""};
            MainApp.Diaries.Add(m_curDiary);
            m_diaryIndex = MainApp.Diaries.Count - 1;
            RefreshLargeViewer();
            MainApp.SaveDiary();
        }

        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            if (m_diaryIndex < 0)
                return;
            MainApp.Diaries[m_diaryIndex].Title = TxtTitle.Text;
            MainApp.Diaries[m_diaryIndex].Content = TxtMain.Text;
            RefreshLargeViewer();
            MainApp.SaveDiary();
        }

        private void BtnEdit_Click(object sender, RoutedEventArgs e)
        {

        }

        private void BtnDel_Click(object sender, RoutedEventArgs e)
        {
            if (m_diaryIndex < 0)
                return;
            MainApp.Diaries.RemoveAt(m_diaryIndex);
            m_diaryIndex = -1;
            m_curDiary = null;
            MainApp.SaveDiary();
            MainApp.SpaceProxy.ShowModalDialog(null, "");
        }
    }
}
